from zope.component import getMultiAdapter
from zope.component import queryMultiAdapter
from zope.component import queryUtility
from plone.portlets.interfaces import IPortletManagerRenderer
from plone.portlets.interfaces import IPortletManager
from plone.app.layout.globals.layout import LayoutPolicy
from plone.memoize.view import memoize

from plone.i18n.normalizer.interfaces import IIDNormalizer
from plone.app.layout.globals import layout as base
from plone.app.layout.navigation.interfaces import INavigationRoot
from Acquisition import aq_base
class BpLayoutPolicy(LayoutPolicy):
    @memoize
    def have_portlets(self, manager_name, view=None):
        if manager_name != 'plone.leftcolumn':
            force_disable = self.request.get('disable_' + manager_name, None)

            if force_disable is not None:
                return not bool(force_disable)

        context = self.context
        if view is None:
            view = self
        manager = queryUtility(IPortletManager, name=manager_name)
        if manager is None:
            return False

        renderer = queryMultiAdapter((context, self.request, view, manager),IPortletManagerRenderer)

        if renderer is None:
            renderer = getMultiAdapter((context, self.request, self, manager),IPortletManagerRenderer)

        return renderer.visible

    def bodyClass(self, template, view):
        """Returns the CSS class to be used on the body tag.
        """

        # Get content parent
        body_class = base.LayoutPolicy.bodyClass(self, template, view)

        # Include context and parent ids as CSS classes on <body>
        normalizer = queryUtility(IIDNormalizer)

        body_class += " context-" + normalizer.normalize(self.context.getId())

        parent = self.context.aq_parent

        # Check that we have a valid parent
        ##if hasattr(parent, "getId"):
        ##    body_class += " parent-" + normalizer.normalize(parent.getId())

        # Get path with "Default content item" wrapping applied
        ##context_helper = getMultiAdapter((self.context, self.request), name="plone_context_state")
        ##canonical = context_helper.canonical_object()

        # Mark site front page with special CSS class
        ##if INavigationRoot.providedBy(canonical):

        ##    if "template-document_view" in body_class:
        ##        body_class += " front-page"

        # Add in logged-in / not logged in status
        portal_state = getMultiAdapter((self.context, self.request), name="plone_portal_state")
        if portal_state.anonymous():
            body_class += " anonymous"
        else:
            body_class += " logged-in"



        rutaList=self.context.getPhysicalPath()

        if "recreacion" in rutaList:
            body_class=body_class.replace("section-intereses","section-recreacion")
        elif "investigacion" in rutaList:
            body_class=body_class.replace("section-intereses","section-investigacion")
        elif "apoyo-a-la-educacion" in rutaList:
            body_class=body_class.replace("section-intereses","section-apoyo")
        elif "curiosidades" in rutaList:
            body_class=body_class.replace("section-intereses","section-curiosidades")

        return body_class
